using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    class clsTHUOC_TINH
    {
        public clsTHUOC_TINH(string sConn)
	    {
		    SqlHelper.ConnectionString = sConn;
	    }
        public List<THUOC_TINH> DataTableToLstObj(DataTable dt)
        {
            THUOC_TINH ttDTO;
            List<THUOC_TINH> lstTtDTO = new List<THUOC_TINH>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ttDTO = new THUOC_TINH();
                try
                {
                    ttDTO._maThuocTinh = int.Parse(dt.Rows[i]["MaThuocTinh"].ToString());
                }
                catch (Exception ex)
                {
                }
                try
                {
                    ttDTO._maLoaiThuocTinh = dt.Rows[i]["MaLoaiThuocTinh"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    ttDTO._giaTriTT = dt.Rows[i]["GiaTriTT"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    ttDTO._maVatTuPhuTung = dt.Rows[i]["MaVTPT"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    ttDTO._tenLoaiTT = dt.Rows[i]["TenLoai"].ToString();
                }
                catch (Exception ex)
                {
                }
                lstTtDTO.Add(ttDTO);
            }
            return lstTtDTO;
        }
        public THUOC_TINH DataTableToObj(DataTable dt)
        {
            THUOC_TINH ttDTO = new THUOC_TINH();
            try
            {
                ttDTO._maThuocTinh = int.Parse(dt.Rows[0]["MaThuocTinh"].ToString());
            }
            catch (Exception ex)
            {
            }
            try
            {
                ttDTO._maLoaiThuocTinh = dt.Rows[0]["MaLoaiThuocTinh"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                ttDTO._giaTriTT = dt.Rows[0]["GiaTriTT"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                ttDTO._maVatTuPhuTung = dt.Rows[0]["MaVTPT"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                ttDTO._tenLoaiTT = dt.Rows[0]["TenLoai"].ToString();
            }
            catch (Exception ex)
            {
            }
            return ttDTO;
        }
        public List<THUOC_TINH> SelectTHUOC_TINHByID(string id)
        {
            DataTable dt = new DataTable();
            List<THUOC_TINH> lstTtDTO = new List<THUOC_TINH>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectTHUOC_TINHByID";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaVTPT", id);
                //dt = SqlHelper.ExecuteToDataTable(spName, CommandType.Text);
                //lst = GetVYLUONG_KHOANLUONG(dt);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstTtDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstTtDTO;
        }
        public List<THUOC_TINH> ShowMaterialPictureForSearch(string id, string maTau)
        {
            DataTable dt = new DataTable();
            List<THUOC_TINH> lstTtDTO = new List<THUOC_TINH>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_ShowMaterialPictureForSearch";
                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@MaCPC", id);
                par[1] = new SqlParameter("@MaTau", maTau);
                //dt = SqlHelper.ExecuteToDataTable(spName, CommandType.Text);
                //lst = GetVYLUONG_KHOANLUONG(dt);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstTtDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstTtDTO;
        }
        public List<THUOC_TINH> SelectMaVTPTByImage(string id)
        {
            DataTable dt = new DataTable();
            List<THUOC_TINH> lstTtDTO = new List<THUOC_TINH>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectMaVTPTByImage";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@Image", id);
                //dt = SqlHelper.ExecuteToDataTable(spName, CommandType.Text);
                //lst = GetVYLUONG_KHOANLUONG(dt);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstTtDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstTtDTO;
        }
        public int InsertUpdateTHUOC_TINH(int? maThuocTinh, string maLoaiThuocTinh, string giaTriTT, string maVTPT)
        {
            int result = 0;
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_InsertUpdateTHUOC_TINH";
                SqlParameter[] par = new SqlParameter[4];
                par[0] = new SqlParameter("@MaThuocTinh", maThuocTinh);
                par[1] = new SqlParameter("@MaLoaiThuocTinh", maLoaiThuocTinh);
                par[2] = new SqlParameter("@GiaTriTT", giaTriTT);
                par[3] = new SqlParameter("@MaVTPT", maVTPT);
                result = SqlHelper.ExecuteNonQuery(spName, par);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return result;
        }
        
    }
}
